%{
    This file is part of StemCellQC, a video bioinformatics software
    toolkit for analysis of phase contrast microscopy videos.
    Copyright 2013-2015 Vincent On. [vincenton001-at-gmail.com]

    StemCellQC is free software: you can redistribute it and/or 
    modify it under the terms of the GNU General Public License as 
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    StemCellQC is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with StemCellQC.  If not, see <http://www.gnu.org/licenses/>.
%}

%Creates tracklets between 2 adjacent frames, done by overlaping objects
%cc1 , cc2 connected component maps of adjacent frames
%cd_limit = limit in the change in distance


function cc_links = connect_cc(cc1,cc2, cd_limit)

cc1cen = regionprops(cc1,'Centroid');
cc2cen = regionprops(cc2,'Centroid');

%initialize links of tracks
cc_links = [];

for j = 1 : cc1.NumObjects %current object in frame 1
    overlap = [];
    for i = 1 : cc2.NumObjects
        common = intersect(cc1.PixelIdxList{j},cc2.PixelIdxList{i});
        
        if ~isempty(common)
            overlap(end + 1) = i;
        end
    end
    
    %if overlap isn't empty, find object with the most overlap
    max_overlap = 0;
    tracklet = [];
    if ~isempty(overlap)
        for k = overlap
           overlap_count = numel( intersect( cc1.PixelIdxList{ j }, cc2.PixelIdxList{ k }));
           
           if max_overlap < overlap_count
               max_overlap = overlap_count;
               tracklet = [j k];
           end
        end
        
        cc_links = [cc_links; tracklet];
        
            
    else    %if there are no overlapping objects, check closest centroid within range
        B = bounding_box(cc1cen(j).Centroid, cc1.ImageSize, cd_limit, cd_limit); %B = [top,bottom,left,right] boundaries of box
        
        %Find all centroids of frame 2 in current bounding box of frame 1
        in_box = [];
        for i = 1 : cc2.NumObjects %check all objects in frame 2
            x = round(cc2cen(i).Centroid(1));
            y = round(cc2cen(i).Centroid(2));
            
            if B(1) <= y && y <= B(2) && B(3) <= x && x <= B(4)
                in_box = [in_box i];
            end
        end
        
        %if bounding box isn't empty find closest centroid and create tracklet
        closest = inf;
        tracklet = [];
        if ~isempty(in_box)
            for k = in_box
                cen_dist = pdist([cc1cen(j).Centroid; cc2cen(k).Centroid]);
                
                if closest > cen_dist
                    closest = cen_dist;
                    tracklet = [j k];
                end
            end
            cc_links = [cc_links; tracklet];
        end
    end
end

